﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class adminstrator_addStudent : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();


    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();
        if (Session["UserName"] == null && Session["UserNo"] == null)//没有经过正常途径打开页面
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='TextAlign/javascript'>" +
              "alert('登录超时（或未登录）,请你重新登录');parent.location.href='../login.aspx'</script>", false);
        if (IsPostBack)
            RadioButtonMan.Checked = true;


    }
    
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        string stuNo = UserNo.Text.Trim();
        string stuName = UserName.Text.Trim();
        string stuSex;
        string stuNation = DropDownListNation.SelectedItem.Text;
        string stuCollege = DropDownListCollege.SelectedItem.Text;

        if (RadioButtonMan.Checked)
            stuSex = "男";
        else
            stuSex = "女";
        string mysql = "select *  from StudentTb where sNo='" + stuNo + "";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myReader = mycmd.ExecuteReader();
        if (myReader.Read())
            LabelError.Text = "*输入的学号已经存在，请重新输入！";
        else
        {
            string myinsert = "insert into StudentTb(sNO,sName,sSex,sNation,sCollege) values" +
                "('" + stuNo + "','" + stuName + "','" + stuSex + "','" + stuNation + "','" + stuCollege + "')";
            mycmd = new OleDbCommand(myinsert, myConnection);
            mycmd.ExecuteScalar();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('学生信息添加成功！');</script>", false);
            UserNo.Text = "";
            UserName.Text = "";
            RadioButtonMan.Checked = true;
            RadioButtonWoman.Checked = false;
            DropDownListNation.SelectedIndex = 0;
            DropDownListCollege.SelectedIndex = 0;
            LabelError.Text = "";
        }
    }
    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        UserNo.Text = "";
        UserName.Text = "";
        RadioButtonMan.Checked = true;
        RadioButtonWoman.Checked = false;//必须把“女”单选择框设置为FALSE
        DropDownListNation.SelectedIndex = 0;
        DropDownListCollege.SelectedIndex = 0;
        LabelError.Text = "";
    }
}